package com.njupt.sellsec.rabbitmq;

import com.njupt.sellsec.domain.SecSellMessage;
import com.njupt.sellsec.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 21:50 2018/7/31
 * @Modified By:
 **/
@Service
public class MQSender {

    @Autowired
    private AmqpTemplate amqpTemplate;

    private static Logger logger = LoggerFactory.getLogger(MQSender.class);


/*    public void send(Object message){

        String msg = RedisService.beanToString(message);
        amqpTemplate.convertAndSend(MQConfig.QUEUE_NAME,msg);
        logger.info("send message:"+msg);
    }

    public void sendTopic1(Object message){
        String msg = RedisService.beanToString(message);
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key1",msg+"1");

        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key2",msg+"2");
        logger.info("topic send message:"+msg);
    }

    public void sendFanout(Object message){
        String msg = RedisService.beanToString(message);
        logger.info("fanout send message");
        amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,"",msg);

    }

    public void sendHeaders(Object message){
        String msg = RedisService.beanToString(message);

        logger.info("header send message:"+msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("header1","value1");
        properties.setHeader("header2","value2");
        Message obj = new Message(msg.getBytes(),properties);
        amqpTemplate.convertAndSend(MQConfig.HEADER_EXCHANGE,"",obj);

    }*/

   public void sendSecSellMessage(SecSellMessage message) {
            String msg = RedisService.beanToString(message);
            amqpTemplate.convertAndSend(MQConfig.SECSELL_QUEUE,msg);
            logger.info("秒杀商品消息:"+msg);
    }
}
